package com.zt.rongda.shelter.controller;

import com.zt.servlet.*;
import com.zt.rongda.shelter.model.Shelter;
import com.zt.rongda.shelter.service.ShelterService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping(value = "/shelter")
public class ShelterController {

    @Autowired
    private ShelterService shelterService;

    @RequestMapping(value = "/query")
    @ResponseBody
    
    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(shelterService.getTable(reqBean));
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        shelterService.add(getShelter(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    
    public JSONObject update(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        shelterService.update(getShelter(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    
    public JSONObject delete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        shelterService.delete(reqBean.getInfo("shelterId"));
        return Response.responseSuccess();
    }

    /**
    * 从表单获取对象
    *
    * @param reqBean
    * @return
    */
    private Shelter getShelter(ReqBean reqBean) {
        Shelter shelter = new Shelter();
        shelter.setShelterId(reqBean.getInfo("shelterId"));
        shelter.setShelterNo(reqBean.getInfo("shelterNo"));
        shelter.setOriginalNo(reqBean.getInfo("originalNo"));
        shelter.setLongitude(reqBean.getInfo("longitude"));
        shelter.setLatitude(reqBean.getInfo("latitude"));
        shelter.setAreaNo(reqBean.getInfo("areaNo"));
        shelter.setCounty(reqBean.getInfo("county"));
        shelter.setStopName(reqBean.getInfo("stopName"));
        shelter.setDeputyStopName(reqBean.getInfo("deputyStopName"));
        shelter.setDirection(reqBean.getInfo("direction"));
        shelter.setEquipmentType(reqBean.getInfo("equipmentType"));
        shelter.setBigBoardNum(reqBean.getInfoInt("bigBoardNum"));
        shelter.setBigBoardSize(reqBean.getInfo("bigBoardSize"));
        shelter.setBigBoardSituation(reqBean.getInfo("bigBoardSituation"));
        shelter.setPowerSupply(reqBean.getInfo("powerSupply"));
        shelter.setAdSize(reqBean.getInfo("adSize"));
        shelter.setCompany(reqBean.getInfo("company"));
        shelter.setOperator(reqBean.getUserId());
        shelter.setRemark(reqBean.getInfo("remark"));
        return shelter;
    }

}